﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using webapp4U.BOL;
using webapp4U;
/// <summary>
/// Summary description for InClassCategoryDAL
/// </summary>

namespace webapp4U.DAL
{
    public class InClassCategoryDAL
    {
        #region ObjIDataReader

        /// <summary>
        /// Returns a new Category instance filled with the DataReader's current record data 
        /// </summary>
        private InClassCategory GetCategoryFromReader(IDataReader dr)
        {
            InClassCategory obj = new InClassCategory();

            obj.InClassKey = (dr["inclasskey"] is DBNull) ? String.Empty : (String)dr["inclasskey"];
            obj.Description = (dr["description"] is DBNull) ? String.Empty : (String)dr["description"];
            obj.CommodDescription = (dr["CommodDescription"] is DBNull) ? String.Empty : (String)dr["CommodDescription"];            
            return obj;
        }
        #endregion

        private List<InClassCategory> GetCategoryCollectionFromReader(IDataReader dr)
        {
            List<InClassCategory> obj = new List<InClassCategory>();

            if (dr != null)
            {
                while (dr.Read())
                    obj.Add(GetCategoryFromReader(dr));
                dr.Close();
            }
            return obj;
        }

        public string GetNameCategoryByInClassKey(string InClassKey, ref string error)
        {
            ClassExecute.ClassExecute obj = new ClassExecute.ClassExecute();
            return obj.ReturnValueFirstColumnOfFirstRow(Configuration.CurrentConnectionString, ref error, "TTr_Products_GetInClassNameByInClassKey",
                CommandType.StoredProcedure, new SqlParameter("@InClassKey", InClassKey));

        }

        public string GetCommodNameCategoryCommodByInClasss(string InClassKey, ref string error)
        {
            ClassExecute.ClassExecute obj = new ClassExecute.ClassExecute();
            return obj.ReturnValueFirstColumnOfFirstRow(Configuration.CurrentConnectionString, ref error, "TTr_Products_GetCommodNameByInClass",
                CommandType.StoredProcedure, new SqlParameter("@InClassKey", InClassKey));

        }

        public string GetCommodNameCategoryCommodByItemKey(string ItemKey, ref string error)
        {
            ClassExecute.ClassExecute obj = new ClassExecute.ClassExecute();
            return obj.ReturnValueFirstColumnOfFirstRow(Configuration.CurrentConnectionString, ref error, "TTr_Products_GetCommodNameByItemKey",
                CommandType.StoredProcedure, new SqlParameter("@ItemKey", ItemKey));

        }

        public string GetInClassNameCategoryCommodByItemKey(string ItemKey, ref string error)
        {
            ClassExecute.ClassExecute obj = new ClassExecute.ClassExecute();
            return obj.ReturnValueFirstColumnOfFirstRow(Configuration.CurrentConnectionString, ref error, "TTr_Products_GetInClassNameByItemKey",
                CommandType.StoredProcedure, new SqlParameter("@ItemKey", ItemKey));

        }

        public string GetNameCategoryByCommodKey(string CommodKey, ref string error)
        {
            ClassExecute.ClassExecute obj = new ClassExecute.ClassExecute();
            return obj.ReturnValueFirstColumnOfFirstRow(Configuration.CurrentConnectionString, ref error, "TTr_Products_GetInClassNameByCommodKey",
                CommandType.StoredProcedure, new SqlParameter("@CommodKey", CommodKey));

        }

        public int GetCountCategory(ref string error)
        {
            ClassExecute.ClassExecute obj = new ClassExecute.ClassExecute();
            return int.Parse(obj.ReturnValueFirstColumnOfFirstRow(Configuration.CurrentConnectionString, ref error, "TTr_Products_GetCountInClass", CommandType.StoredProcedure));                   
                        
        }

        public int GetCountCategoryCommodNotInClass(ref string error)
        {            
            ClassExecute.ClassExecute obj = new ClassExecute.ClassExecute();
            
            return int.Parse(obj.ReturnValueFirstColumnOfFirstRow(Configuration.CurrentConnectionString, ref error, "TTr_Products_GetCountInCommdeNotInClass", CommandType.StoredProcedure));
        }

        public int GetCountCategoryByCommoKey(string CommodKey, ref string error)
        {
            ClassExecute.ClassExecute obj = new ClassExecute.ClassExecute();
            return int.Parse(obj.ReturnValueFirstColumnOfFirstRow(Configuration.CurrentConnectionString, ref error, "TTr_Products_GetCountInClassByInCommodKey", 
                CommandType.StoredProcedure, new SqlParameter("@CommodKey", CommodKey)));

        }

        public List<InClassCategory> GetAllCategoryByCommodKye(string CommodKey, int PageIndex, int PageSize, ref string error)
        {
            ClassExecute.ClassExecute obj = new ClassExecute.ClassExecute();
            SqlDataReader dr = obj.Get_Data_DataReader(Configuration.CurrentConnectionString, ref error, "TTr_InClass_GetInClassByCommodKey",
            CommandType.StoredProcedure, new SqlParameter("@CommodKey", CommodKey), new SqlParameter("@PageIndex", PageIndex), new SqlParameter("@PageSize", PageSize));
            return GetCategoryCollectionFromReader(dr);

        }

        public List<InClassCategory> GetAllCategory(int PageIndex, int PageSize, ref string error)
        {
            ClassExecute.ClassExecute obj = new ClassExecute.ClassExecute();
            SqlDataReader dr = obj.Get_Data_DataReader(Configuration.CurrentConnectionString, ref error, "TTr_InClass_GetAllInClass",
            CommandType.StoredProcedure, new SqlParameter("@PageIndex", PageIndex), new SqlParameter("@PageSize", PageSize));
            return GetCategoryCollectionFromReader(dr);
            
        }

        public List<InClassCategory> GetAllCategoryCommodNotInClass(int PageIndex, int PageSize, ref string error)
        {
            ClassExecute.ClassExecute obj = new ClassExecute.ClassExecute();
            SqlDataReader dr = obj.Get_Data_DataReader(Configuration.CurrentConnectionString, ref error, "TTr_InCommod_GetAllInCommod",
            CommandType.StoredProcedure, new SqlParameter("@PageIndex", PageIndex), new SqlParameter("@PageSize", PageSize));
            return GetCategoryCollectionFromReader(dr);

        }
        public List<InClassCategory> GetInClassCategoryNoPageSize(ref string error)
        {
            ClassExecute.ClassExecute obj = new ClassExecute.ClassExecute();
            SqlDataReader dr = obj.Get_Data_DataReader(Configuration.CurrentConnectionString, ref error, "TTr_GetInClassCategory",
            CommandType.StoredProcedure);           
            return GetCategoryCollectionFromReader(dr);
        }
    }
}